package com.zbkj.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zbkj.common.model.member.UserElectronicInvoiceConfig;
import com.zbkj.service.dao.UserElectronicInvoiceConfigDao;
import com.zbkj.service.service.UserElectronicInvoiceConfigService;
import com.zbkj.service.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 用户发票设置
 */
@Slf4j
@Service
public class UserElectronicInvoiceConfigServiceImpl
        extends ServiceImpl<UserElectronicInvoiceConfigDao, UserElectronicInvoiceConfig>
        implements UserElectronicInvoiceConfigService {
    @Autowired
    private UserService userService;
    @Resource
    private UserElectronicInvoiceConfigDao dao;

    /**
     * 添加发票设置
     *
     * @param userElectronicInvoiceConfig
     * @return
     */
    @Override
    public UserElectronicInvoiceConfig add(UserElectronicInvoiceConfig userElectronicInvoiceConfig) {
        log.info("add:{}", userElectronicInvoiceConfig.toString());
        userElectronicInvoiceConfig.setUid(String.valueOf(userService.getUserId()));
        int count = dao.selectCount(new LambdaQueryWrapper<UserElectronicInvoiceConfig>()
                .eq(UserElectronicInvoiceConfig::getUid, userService.getUserId()));
        if (userElectronicInvoiceConfig.getStatus() == null) {
            userElectronicInvoiceConfig.setStatus(0);
        }
        if (count == 0) {
            //收个设置 默认
            userElectronicInvoiceConfig.setStatus(1);
        }
        int status = userElectronicInvoiceConfig.getStatus();
        Date d = new Date();
        userElectronicInvoiceConfig.setCreateTime(d);
        userElectronicInvoiceConfig.setUpdateTime(d);
        dao.insert(userElectronicInvoiceConfig);
        if (status == 1) {
            //设置默认
            setStatus(userElectronicInvoiceConfig.getId());
        }
        return userElectronicInvoiceConfig;
    }

    /**
     * 修改发票设置
     *
     * @param userElectronicInvoiceConfig
     * @return
     */
    @Override
    public UserElectronicInvoiceConfig set(UserElectronicInvoiceConfig userElectronicInvoiceConfig) {
        log.info("set:{}", userElectronicInvoiceConfig.toString());
        if (userElectronicInvoiceConfig.getId() == null) {
            throw new UsernameNotFoundException("地址id不能为空");
        }
        UserElectronicInvoiceConfig data = get(userElectronicInvoiceConfig.getId());
        if (!data.getUid().equals(String.valueOf(userService.getUserId()))) {
            throw new UsernameNotFoundException("非本人发票配置，无法修改");
        }
        if (userElectronicInvoiceConfig.getStatus() == null) {
            userElectronicInvoiceConfig.setStatus(0);
        }
        int status = userElectronicInvoiceConfig.getStatus();
        Date d = new Date();
        userElectronicInvoiceConfig.setUpdateTime(d);
        dao.updateById(userElectronicInvoiceConfig);
        if (status == 1) {
            //设置默认
            setStatus(userElectronicInvoiceConfig.getId());
        }
        return userElectronicInvoiceConfig;
    }

    /**
     * 设置默认发票
     *
     * @param id Integer
     * @return Integer
     */
    @Override
    public Integer setStatus(Integer id) {
        String userId = String.valueOf(userService.getUserId());
        // 清空默认设置
        LambdaUpdateWrapper<UserElectronicInvoiceConfig> up = Wrappers.lambdaUpdate();
        up.set(UserElectronicInvoiceConfig::getStatus, 0);
        up.eq(UserElectronicInvoiceConfig::getUid, userId);
        update(up);
        //设置默认
        LambdaUpdateWrapper<UserElectronicInvoiceConfig> upStatus = Wrappers.lambdaUpdate();
        upStatus.set(UserElectronicInvoiceConfig::getStatus, 1);
        upStatus.eq(UserElectronicInvoiceConfig::getId, id);
        update(upStatus);
        return 1;
    }

    /**
     * 获取指定发票设置
     *
     * @param id
     * @return
     */
    @Override
    public UserElectronicInvoiceConfig get(Integer id) {
        String userId = String.valueOf(userService.getUserId());
        LambdaQueryWrapper<UserElectronicInvoiceConfig> qw = Wrappers.lambdaQuery();
        qw.eq(UserElectronicInvoiceConfig::getUid, userId);
        qw.eq(UserElectronicInvoiceConfig::getId, id);
        return dao.selectOne(qw);
    }

    /**
     * 获取默认发票设置
     *
     * @return
     */
    @Override
    public UserElectronicInvoiceConfig getDefault() {
        String userId = String.valueOf(userService.getUserId());
        LambdaQueryWrapper<UserElectronicInvoiceConfig> qw = Wrappers.lambdaQuery();
        qw.eq(UserElectronicInvoiceConfig::getUid, userId);
        qw.eq(UserElectronicInvoiceConfig::getStatus, 1);
        return dao.selectOne(qw);
    }

    /**
     * 获取发票设置列表
     *
     * @param userElectronicInvoiceConfig
     * @return
     */
    @Override
    public List<UserElectronicInvoiceConfig> getList(UserElectronicInvoiceConfig userElectronicInvoiceConfig) {
        String userId = String.valueOf(userService.getUserId());
        LambdaQueryWrapper<UserElectronicInvoiceConfig> lqw = Wrappers.lambdaQuery();
        lqw.eq(UserElectronicInvoiceConfig::getUid, userId);
//        lqw.eq(UserElectronicInvoiceConfig::getStatus, true);
        lqw.orderByDesc(UserElectronicInvoiceConfig::getId);

        return dao.selectList(lqw);
    }


}
